package com.chen.nongansampling;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.chen.nongansampling.app.BaseApplication;
import com.chen.nongansampling.bean.LoginRequest;
import com.chen.nongansampling.model.Account;
import com.chen.nongansampling.model.CallbackData;
import com.chen.nongansampling.presenter.LoginPresenter;
import com.chen.nongansampling.presenterView.BaseView;
import com.chen.nongansampling.ui.login.LoginActivity;
import com.chen.nongansampling.utils.StorageUtil;

public class ADActivity extends AppCompatActivity implements BaseView {
    private LoginPresenter loginPresenter=new LoginPresenter(this);

    private  Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            next();
        }
    };

    private void next() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);

        loginPresenter.onCreate();
        loginPresenter.BindPresentView(this);

        LoginRequest loginRequest=new LoginRequest();


        if (StorageUtil.getLoginStatus(getApplicationContext())&&StorageUtil.getAccount(getApplicationContext())!=null&&StorageUtil.getPassword(getApplicationContext())!=null)
        {
            loginRequest.setLoginName(StorageUtil.getAccount(getApplicationContext()));
            loginRequest.setLoginPassword(StorageUtil.getPassword(getApplicationContext()));
            loginPresenter.getLoginResponseInfo(loginRequest);
        }



        //去掉状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //延时3秒发送一个消息给主进程,让主进程执行next()方法
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);

            }
        }, 2000);


    }

    @Override
    public void onError(String result) {

    }

    @Override
    public void onSuccess(Object resultData) {


        CallbackData<Account> account=(CallbackData<Account>)resultData;
        if (account.getData()!=null)
        {
            BaseApplication.setAccount(account.getData());
            handler.removeMessages(0);
            StorageUtil.StoragePasswordAndAccount(account.getData().getLoginName(),account.getData().getLoginPassword(),getApplicationContext());
            BaseApplication.setAccount(account.getData());
            //   StorageUtil.getAccountPkId(account=);
            Intent intent = new Intent(ADActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
//                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                startActivity(intent);
//                finish();
//
        }else {
            Toast.makeText(this,"登陆失败",Toast.LENGTH_LONG).show();

        }
    }
}
